package com.person.jc.service.base.car;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.person.jc.service.Service;
import com.person.jc.utils.Utils;


public class BdCarInfoService extends Service {
	
	public static final BdCarInfoService me = new BdCarInfoService();
	
	public List<Record> getList(Map<String,Object> map){
		String temp = "";
		List<Object> params = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer("select * from bd_car_info where if_valid = 1");
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("carModel")))){
			sql.append(" and car_model like '%"+ temp +"%'");
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("carCode")))){
			sql.append(" and car_code like '%"+ temp +"%'");
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("productionDate")))){
			sql.append(" and DATE_FORMAT(production_date,'%Y-%m-%d') >= ?");
			params.add(temp);
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("productionDate")))){
			sql.append(" and DATE_FORMAT(production_date,'%Y-%m-%d') <= ?");
			params.add(temp);
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("carId")))){
			sql.append(" and car_id = '"+ temp +"'");
		}
		sql.append(" order by car_code ");
		return Db.find(sql.toString(), Utils.getObjParam(params));
	}
	
	public void save(Map<String,Object> map){
		String sql = "insert into bd_car_info (car_model, car_code, production_date, service_cycle) values (?, ?, ?, ?)";
		Db.update(sql, map.get("carModel"), map.get("carCode"), map.get("productionDate"), map.get("serviceCycle"));
		super.setCRUD("车辆管理", "C", "新增车辆信息，车牌号："+map.get("carCode"));
	}
	
	public void update(Map<String,Object> map){
		String sql = " update bd_car_info set car_model = ? , car_code = ?, production_date = ?, service_cycle = ? where car_id = ?";
		Db.update(sql, map.get("carModel"), map.get("carCode"), map.get("productionDate"), map.get("serviceCycle"), map.get("carId"));
		super.setCRUD("车辆管理", "U", "修改车辆信息，车辆编号："+map.get("carId"));
	}
	
	public void delete(String carId){
		String sql = " update bd_car_info set if_valid = 0 where car_id = ? ";
		Db.update(sql, carId);
		super.setCRUD("车辆管理", "D", "删除车辆信息，车辆编号："+carId);
	}
	
	public long checkName(String carCode, String carId){
		String sql = "select count(1) from bd_car_info where car_code = ? and if_valid <> 0 ";
		if(Utils.isNotEmpty(carId)){
			sql += " and car_id <> " + carId;
		}
		return Db.queryLong(sql, carCode);
	}
}
